home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / dsp / dspkgctr.z / dspkgctr / gcc / genrecog.c < prev    next >
C/C++ Source or Header  |  1992-06-08  |  26KB  |  1,106 lines

  1. /* Generate code from machine description to emit insns as rtl.
  2.    Copyright (C) 1987,1988 Free Software Foundation, Inc.
  3.  
  4.    $Id: genrecog.c,v 1.2 91/08/06 10:00:36 jeff Exp $
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* This program is used to produce insn-recog.c, which contains
  24.    a function called `recog' plus its subroutines.
  25.    These functions contain a decision tree
  26.    that recognizes whether an rtx, the argument given to recog,
  27.    is a valid instruction.
  28.  
  29.    recog returns -1 if the rtx is not valid.
  30.    If the rtx is valid, recog returns a nonnegative number
  31.    which is the insn code number for the pattern that matched.
  32.    This is the same as the order in the machine description of the
  33.    entry that matched.  This number can be used as an index into
  34.    insn_templates and insn_n_operands (found in insn-output.c)
  35.    or as an argument to output_insn_hairy (also in insn-output.c).  */
  36.  
  37. #include <stdio.h>
  38. #include "config.h"
  39. #include "rtl.h"
  40. #include "obstack.h"
  41.  
  42. struct obstack obstack;
  43. struct obstack *rtl_obstack = &obstack;
  44.  
  45. #define obstack_chunk_alloc xmalloc
  46. #define obstack_chunk_free free
  47. extern int xmalloc ();
  48. extern void free ();
  49.  
  50. /* Data structure for decision tree for recognizing
  51.    legitimate instructions.  */
  52.  
  53. struct decision
  54. {
  55.   int number;
  56.   char *position;
  57.   RTX_CODE code;
  58.   char *exact;
  59.   enum machine_mode mode;
  60.   char *tests;
  61.   int insn_code_number;
  62.   struct decision *next;
  63.   struct decision *success;
  64.   int opno;
  65.   int dupno;
  66.   int dupcount;
  67.   int test_elt_zero_int;
  68.   int elt_zero_int;
  69.   int test_elt_one_int;
  70.   int elt_one_int;
  71.   int ignmode;
  72.   struct decision *afterward;
  73.   int label_needed;
  74.   char *c_test;
  75.   char *reg_class;
  76.   char enforce_mode;
  77.   int veclen;
  78.   int subroutine_number;
  79. };
  80.  
  81. #define SUBROUTINE_THRESHOLD 50
  82.  
  83. int next_subroutine_number;
  84.  
  85. /*
  86. recognize (top)
  87. {
  88.  staten:
  89.   x = XVECEXP (top, 0, 3);
  90.   if (test_code (GET_CODE (x))
  91.       && test_mode (MODE (x))
  92.       && whatever_else)
  93.     goto statep;
  94.   else if (next one...)
  95.     goto statem:
  96.   goto stater;
  97.  
  98.  statep:
  99.   actions...;
  100.   return 1;
  101.  
  102.  statem:
  103.   x = stack[depth--];
  104.   more tests...;
  105.  
  106.  stateq:
  107.   stack[++depth] = x;
  108.   x = XEXP (stack[depth], 0);
  109.   more tests...;
  110.  
  111.  stater:
  112.   x = XEXP (stack[depth], 1);
  113. }
  114.  
  115. */
  116.  
  117. int next_number;
  118.  
  119. int next_insn_code;
  120.  
  121. /* Number of MATCH_DUP's seen so far in this instruction.  */
  122. int dupcount;
  123.  
  124. struct decision *add_to_sequence ();
  125. struct decision *try_merge_2 ();
  126. void write_subroutine ();
  127. void print_code ();
  128. void clear_codes ();
  129. void clear_modes ();
  130. void change_state ();
  131. void write_tree ();
  132. char *copystr ();
  133. char *concat ();
  134. void fatal ();
  135. void fancy_abort ();
  136. void mybzero ();
  137.  
  138. struct decision *first;
  139.  
  140. /* Construct and return a sequence of decisions
  141.    that will recognize INSN.  */
  142.  
  143. struct decision *
  144. make_insn_sequence (insn)
  145.      rtx insn;
  146. {
  147.   rtx x;
  148.   char *c_test = XSTR (insn, 2);
  149.   struct decision *last;
  150.  
  151.   dupcount = 0;
  152.  
  153.   if (XVECLEN (insn, 1) == 1)
  154.     x = XVECEXP (insn, 1, 0);
  155.   else
  156.     {
  157.       x = rtx_alloc (PARALLEL);
  158.       XVEC (x, 0) = XVEC (insn, 1);
  159.       PUT_MODE (x, VOIDmode);
  160.     }
  161.  
  162.   last = add_to_sequence (x, 0, "");
  163.  
  164.   if (c_test[0])
  165.     last->c_test = c_test;
  166.   last->insn_code_number = next_insn_code++;
  167.  
  168.   return first;
  169. }
  170.  
  171. struct decision *
  172. add_to_sequence (pattern, last, position)
  173.      rtx pattern;
  174.      struct decision *last;
  175.      char *position;
  176. {
  177.   register RTX_CODE code;
  178.   register struct decision *new
  179.     = (struct decision *) xmalloc (sizeof (struct decision));
  180.   struct decision *this;
  181.   char *newpos;
  182.   register char *fmt;
  183.   register int i;
  184.   int depth;
  185.   int len;
  186.  
  187.   new->number = next_number++;
  188.   new->position = copystr (position);
  189.   new->exact = 0;
  190.   new->next = 0;
  191.   new->success = 0;
  192.   new->insn_code_number = -1;
  193.   new->tests = 0;
  194.   new->opno = -1;
  195.   new->dupno = -1;
  196.   new->dupcount = -1;
  197.   new->test_elt_zero_int = 0;
  198.   new->test_elt_one_int = 0;
  199.   new->elt_zero_int = 0;
  200.   new->elt_one_int = 0;
  201.   new->enforce_mode = 0;
  202.   new->ignmode = 0;
  203.   new->afterward = 0;
  204.   new->label_needed = 0;
  205.   new->c_test = 0;
  206.   new->reg_class = 0;
  207.   new->veclen = 0;
  208.   new->subroutine_number = 0;
  209.  
  210.   this = new;
  211.  
  212.   if (last == 0)
  213.     first = new;
  214.   else
  215.     last->success = new;
  216.  
  217.   depth = strlen (position);
  218.   newpos = (char *) alloca (depth + 2);
  219.   strcpy (newpos, position);
  220.   newpos[depth + 1] = 0;
  221.  
  222.  restart:
  223.  
  224.   if (pattern == 0)
  225.     {
  226.       new->exact = "0";
  227.       new->code = UNKNOWN;
  228.       new->mode = VOIDmode;
  229.       return new;
  230.     }
  231.  
  232.   switch (GET_MODE (pattern))
  233.     {
  234.     case 0:
  235.       new->mode = VOIDmode;
  236.       break;
  237.  
  238.     default:
  239.       new->mode = GET_MODE (pattern);
  240.       break;
  241.     }
  242.  
  243.   new->code = code = GET_CODE (pattern);
  244.  
  245.   switch (code)
  246.     {
  247.     case MATCH_OPERAND:
  248.       new->opno = XINT (pattern, 0);
  249.       new->code = UNKNOWN;
  250.       new->tests = XSTR (pattern, 1);
  251.       if (*new->tests == 0)
  252.     new->tests = 0;
  253.       new->reg_class = XSTR (pattern, 2);
  254.       if (*new->reg_class == 0)
  255.     new->reg_class = 0;
  256.       return new;
  257.  
  258.     case MATCH_OPERATOR:
  259.       new->opno = XINT (pattern, 0);
  260.       new->code = UNKNOWN;
  261.       new->tests = XSTR (pattern, 1);
  262.       if (*new->tests == 0)
  263.     new->tests = 0;
  264.       for (i = 0; i < XVECLEN (pattern, 2); i++)
  265.     {
  266.       newpos[depth] = i + '0';
  267.       new = add_to_sequence (XVECEXP (pattern, 2, i), new, newpos);
  268.     }
  269.       this->success->enforce_mode = 0;
  270.       return new;
  271.  
  272.     case MATCH_DUP:
  273.       new->dupno = XINT (pattern, 0);
  274.       new->dupcount = dupcount++;
  275.       new->code = UNKNOWN;
  276.       return new;
  277.  
  278.     case ADDRESS:
  279.       pattern = XEXP (pattern, 0);
  280.       goto restart;
  281.  
  282.     case PC:
  283.       new->exact = "pc_rtx";
  284.       return new;
  285.  
  286.     case CC0:
  287.       new->exact = "cc0_rtx";
  288.       return new;
  289.  
  290.     case CONST_INT:
  291.       if (INTVAL (pattern) == 0)
  292.     {
  293.       new->exact = "const0_rtx";
  294.       return new;
  295.     }
  296.       if (INTVAL (pattern) == 1)
  297.     {
  298.       new->exact = "const1_rtx";
  299.       return new;
  300.     }
  301.       break;
  302.  
  303.     case SET:
  304.       newpos[depth] = '0';
  305.       new = add_to_sequence (SET_DEST (pattern), new, newpos);
  306.       this->success->enforce_mode = 1;
  307.       newpos[depth] = '1';
  308.       new = add_to_sequence (SET_SRC (pattern), new, newpos);
  309.       return new;
  310.  
  311.     case STRICT_LOW_PART:
  312.       newpos[depth] = '0';
  313.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  314.       this->success->enforce_mode = 1;
  315.       return new;
  316.  
  317.     case SUBREG:
  318.       this->test_elt_one_int = 1;
  319.       this->elt_one_int = XINT (pattern, 1);
  320.       newpos[depth] = '0';
  321.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  322.       this->success->enforce_mode = 1;
  323.       return new;
  324.  
  325.     case ZERO_EXTRACT:
  326.     case SIGN_EXTRACT:
  327.       newpos[depth] = '0';
  328.       new = add_to_sequence (XEXP (pattern, 0), new, newpos);
  329.       this->success->enforce_mode = 1;
  330.       newpos[depth] = '1';
  331.       new = add_to_sequence (XEXP (pattern, 1), new, newpos);
  332.       newpos[depth] = '2';
  333.       new = add_to_sequence (XEXP (pattern, 2), new, newpos);
  334.       return new;
  335.     }
  336.  
  337.   fmt = GET_RTX_FORMAT (code);
  338.   len = GET_RTX_LENGTH (code);
  339.   for (i = 0; i < len; i++)
  340.     {
  341.       newpos[depth] = '0' + i;
  342.       if (fmt[i] == 'e' || fmt[i] == 'u')
  343.     new = add_to_sequence (XEXP (pattern, i), new, newpos);
  344.       else if (fmt[i] == 'i' && i == 0)
  345.     {
  346.       this->test_elt_zero_int = 1;
  347.       this->elt_zero_int = XINT (pattern, i);
  348.     }
  349.       else if (fmt[i] == 'i' && i == 1)
  350.     {
  351.       this->test_elt_one_int = 1;
  352.       this->elt_one_int = XINT (pattern, i);
  353.     }
  354.       else if (fmt[i] == 'E')
  355.     {
  356.       register int j;
  357.       /* We do not handle a vector appearing as other than
  358.          the first item, just because nothing uses them
  359.          and by handling only the special case
  360.          we can use one element in newpos for either
  361.          the item number of a subexpression
  362.          or the element number in a vector.  */
  363.       if (i != 0)
  364.         abort ();
  365.       this->veclen = XVECLEN (pattern, i);
  366.       for (j = 0; j < XVECLEN (pattern, i); j++)
  367.         {
  368.           newpos[depth] = 'a' + j;
  369.           new = add_to_sequence (XVECEXP (pattern, i, j),
  370.                      new, newpos);
  371.         }
  372.     }
  373.       else if (fmt[i] != '0')
  374.     abort ();
  375.     }
  376.   return new;
  377. }
  378.  
  379. /* Merge two decision trees OLD and ADD,
  380.    modifying OLD destructively,
  381.    and return the merged tree.  */
  382.  
  383. struct decision *
  384. merge_trees (old, add)
  385.      register struct decision *old, *add;
  386. {
  387.   while (add)
  388.     {
  389.       register struct decision *next = add->next;
  390.       add->next = 0;
  391.       if (!try_merge_1 (old, add))
  392.     old = try_merge_2 (old, add);
  393.       add = next;
  394.     }
  395.   return old;
  396. }
  397.  
  398. /* Merge ADD into the next-chain starting with OLD
  399.    only if it overlaps a condition already tested in OLD.
  400.    Returns 1 if successful (OLD is modified),
  401.    0 if nothing has been done.  */
  402.  
  403. int
  404. try_merge_1 (old, add)
  405.      register struct decision *old, *add;
  406. {
  407.   while (old)
  408.     {
  409.       if ((old->position == add->position
  410.        || (old->position && add->position
  411.            && !strcmp (old->position, add->position)))
  412.       && (old->tests == add->tests
  413.           || (old->tests && add->tests && !strcmp (old->tests, add->tests)))
  414.       && (old->c_test == add->c_test
  415.           || (old->c_test && add->c_test && !strcmp (old->c_test, add->c_test)))
  416.       && old->test_elt_zero_int == add->test_elt_zero_int
  417.       && old->elt_zero_int == add->elt_zero_int
  418.       && old->test_elt_one_int == add->test_elt_one_int
  419.       && old->elt_one_int == add->elt_one_int
  420.       && old->veclen == add->veclen
  421.       && old->dupno == add->dupno
  422.       && old->opno == add->opno
  423.       && (old->tests == 0
  424.           || (add->enforce_mode ? no_same_mode (old) : old->next == 0))
  425.       && old->code == add->code
  426.       && old->mode == add->mode)
  427.     {
  428.       old->success = merge_trees (old->success, add->success);
  429.       if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
  430.         fatal ("Two actions at one point in tree.");
  431.       if (old->insn_code_number == -1)
  432.         old->insn_code_number = add->insn_code_number;
  433.       return 1;
  434.     }
  435.       old = old->next;
  436.     }
  437.   return 0;
  438. }
  439.  
  440. /* Merge ADD into the next-chain that starts with OLD,
  441.    preferably after something that tests the same place
  442.    that ADD does.
  443.    The next-chain of ADD itself is ignored, and it is set
  444.    up for entering ADD into the new chain.
  445.    Returns the new chain.  */
  446.  
  447. struct decision *
  448. try_merge_2 (old, add)
  449.      struct decision *old, *add;
  450. {
  451.   register struct decision *p;
  452.   struct decision *last = 0;
  453.   struct decision *last_same_place = 0;
  454.  
  455.   /* Put this in after the others that test the same place,
  456.      if there are any.  If not, find the last chain element
  457.      and insert there.
  458.  
  459.      One modification: if this one is NOT a MATCH_OPERAND,
  460.      put it before any MATCH_OPERANDS that test the same place.
  461.  
  462.      Another: if enforce_mode (i.e. this is first operand of a SET),
  463.      put this after the last thing that tests the same place for
  464.      the same mode.  */
  465.  
  466.   int operand = 0 != add->tests;
  467.  
  468.   for (p = old; p; p = p->next)
  469.     {
  470.       if (p->position == add->position
  471.       || (p->position && add->position
  472.           && !strcmp (p->position, add->position)))
  473.     {
  474.       last_same_place = p;
  475.       /* If enforce_mode, segregate the modes in numerical order.  */
  476.       if (p->enforce_mode && (int) add->mode < (int) p->mode)
  477.         break;
  478. #if 0
  479.       /* Keep explicit decompositions before those that test predicates.
  480.          If enforce_mode, do this separately within each mode.  */
  481.       if (! p->enforce_mode || p->mode == add->mode)
  482.         if (!operand && p->tests)
  483.           break;
  484. #endif
  485.     }
  486.       /* If this is past the end of the decisions at the same place as ADD,
  487.      stop looking now; add ADD before here.  */
  488.       else if (last_same_place)
  489.     break;
  490.       last = p;
  491.     }
  492.  
  493.   /* Insert before P, which means after LAST.  */
  494.  
  495.   if (last)
  496.     {
  497.       add->next = last->next;
  498.       last->next = add;
  499.       return old;
  500.     }
  501.  
  502.   add->next = old;
  503.   return add;
  504. }
  505.  
  506. int
  507. no_same_mode (node)
  508.      struct decision *node;
  509. {
  510.   register struct decision *p;
  511.   register enum machine_mode mode = node->mode;
  512.  
  513.   for (p = node->next; p; p = p->next)
  514.     if (p->mode == mode)
  515.       return 0;
  516.  
  517.   return 1;
  518. }
  519.  
  520. /* Count the number of subnodes of node NODE, assumed to be the start
  521.    of a next-chain.  If the number is high enough, make NODE start
  522.    a separate subroutine in the C code that is generated.  */
  523.  
  524. int
  525. break_out_subroutines (node)
  526.      struct decision *node;
  527. {
  528.   int size = 0;
  529.   struct decision *sub;
  530.   for (sub = node; sub; sub = sub->next)
  531.     size += 1 + break_out_subroutines (sub->success);
  532.   if (size > SUBROUTINE_THRESHOLD)
  533.     {
  534.       node->subroutine_number = ++next_subroutine_number;
  535.       write_subroutine (node);
  536.       size = 1;
  537.     }
  538.   return size;
  539. }
  540.  
  541. void
  542. write_subroutine (tree)
  543.      struct decision *tree;
  544. {
  545.   printf ("int\nrecog_%d (x0, insn)\n     register rtx x0;\n     rtx insn;\n{\n",
  546.       tree->subroutine_number);
  547.   printf ("  register rtx x1, x2, x3, x4, x5;\n  rtx x6, x7, x8, x9, x10, x11;\n");
  548.   printf ("  int tem;\n");
  549.   write_tree (tree, "", 0, "", 1);
  550.   printf (" ret0: return -1;\n}\n\n");
  551. }
  552.  
  553. /* Write out C code to perform the decisions in the tree.  */
  554.  
  555. void
  556. write_tree (tree, prevpos, afterward, afterpos, initial)
  557.      struct decision *tree;
  558.      char *prevpos;
  559.      int afterward;
  560.      char *afterpos;
  561.      int initial;
  562. {
  563.   register struct decision *p, *p1;
  564.   char *pos;
  565.   register int depth;
  566.   int ignmode;
  567.   enum anon1 { NO_SWITCH, CODE_SWITCH, MODE_SWITCH } in_switch = NO_SWITCH;
  568.   char modemap[NUM_MACHINE_MODES];
  569.   char codemap[NUM_RTX_CODE];
  570.  
  571.   pos = prevpos;
  572.  
  573.   if (tree->subroutine_number > 0 && ! initial)
  574.     {
  575.       printf (" L%d:\n", tree->number);
  576.  
  577.       if (afterward)
  578.     {
  579.       printf ("  tem = recog_%d (x0, insn);\n",
  580.           tree->subroutine_number);
  581.       printf ("  if (tem >= 0) return tem;\n");
  582.       change_state (pos, afterpos);
  583.       printf ("  goto L%d;\n", afterward);
  584.     }
  585.       else
  586.     printf ("  return recog_%d (x0, insn);\n",
  587.         tree->subroutine_number);
  588.       return;
  589.     }
  590.  
  591.   tree->label_needed = 1;
  592.   for (p = tree; p; p = p->next)
  593.     {
  594.       /* Find the next alternative to p
  595.      that might be true when p is true.
  596.      Test that one next if p's successors fail.
  597.      Note that when the `tests' field is nonzero
  598.      it is up to the specified test-function to compare machine modes
  599.      and some (such as general_operand) don't always do so.
  600.      But when inside a switch-on-modes we ignore this and
  601.      consider all modes mutually exclusive.  */
  602.       for (p1 = p->next; p1; p1 = p1->next)
  603.     if (((p->code == UNKNOWN || p1->code == UNKNOWN || p->code == p1->code)
  604.          && (p->mode == VOIDmode || p1->mode == VOIDmode
  605.          || p->mode == p1->mode
  606.          || (in_switch != MODE_SWITCH && (p->tests || p1->tests))))
  607.         || strcmp (p1->position, p->position))
  608.       break;
  609.       p->afterward = p1;
  610.       if (p1) p1->label_needed = 1;
  611.  
  612.       if (in_switch == MODE_SWITCH
  613.       && (p->mode == VOIDmode || (! p->enforce_mode && p->tests != 0)))
  614.     {
  615.       in_switch = NO_SWITCH;
  616.       printf ("  }\n");
  617.     }
  618.       if (in_switch == CODE_SWITCH && p->code == UNKNOWN)
  619.     {
  620.       in_switch = NO_SWITCH;
  621.       printf ("  }\n");
  622.     }
  623.  
  624.       if (p->label_needed)
  625.     printf (" L%d:\n", p->number);
  626.  
  627.       if (p->success == 0 && p->insn_code_number < 0)
  628.     abort ();
  629.  
  630.       change_state (pos, p->position);
  631.       pos = p->position;
  632.       depth = strlen (pos);
  633.  
  634.       ignmode = p->ignmode || pos[depth - 1] == '*' || p->tests;
  635.  
  636.       if (in_switch == NO_SWITCH)
  637.     {
  638.       /* If p and its alternatives all want the same mode,
  639.          reject all others at once, first, then ignore the mode.  */
  640.       if (!ignmode && p->mode != VOIDmode && p->next && same_modes (p, p->mode))
  641.         {
  642.           printf ("  if (GET_MODE (x%d) != %smode)\n",
  643.               depth, GET_MODE_NAME (p->mode));
  644.           if (afterward)
  645.         {
  646.           printf ("    {\n    ");
  647.           change_state (pos, afterpos);
  648.           printf ("      goto L%d;\n    }\n", afterward);
  649.         }
  650.           else
  651.         printf ("    goto ret0;\n");
  652.           clear_modes (p);
  653.           ignmode = 1;
  654.         }
  655.  
  656.       /* If p and its alternatives all want the same code,
  657.          reject all others at once, first, then ignore the code.  */
  658.       if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
  659.         {
  660.           printf ("  if (GET_CODE (x%d) != ", depth);
  661.           print_code (p->code);
  662.           printf (")\n");
  663.           if (afterward)
  664.         {
  665.           printf ("    {");
  666.           change_state (pos, afterpos);
  667.           printf ("    goto L%d; }\n", afterward);
  668.         }
  669.           else
  670.         printf ("    goto ret0;\n");
  671.           clear_codes (p);
  672.         }
  673.     }
  674.  
  675.       /* If p and its alternatives all have different modes
  676.      and there are at least 4 of them, make a switch.  */
  677.       if (in_switch == NO_SWITCH && pos[depth-1] != '*')
  678.     {
  679.       register int i;
  680.       int lose = 0;
  681.  
  682.       mybzero (modemap, sizeof modemap);
  683.       for (p1 = p, i = 0;
  684.            (p1 && p1->mode != VOIDmode
  685.         && (p1->tests == 0 || p1->enforce_mode));
  686.            p1 = p1->next, i++)
  687.         {
  688.           if (! p->enforce_mode && modemap[(int) p1->mode])
  689.         {
  690.           lose = 1;
  691.           break;
  692.         }
  693.           modemap[(int) p1->mode] = 1;
  694.         }
  695.       if (!lose && i >= 4)
  696.         {
  697.           in_switch = MODE_SWITCH;
  698.           printf (" switch (GET_MODE (x%d))\n  {\n", depth);
  699.         }
  700.     }
  701.  
  702.       if (in_switch == NO_SWITCH)
  703.     {
  704.       register int i;
  705.       mybzero (codemap, sizeof codemap);
  706.       for (p1 = p, i = 0; p1 && p1->code != UNKNOWN; p1 = p1->next, i++)
  707.         {
  708.           if (codemap[(int) p1->code])
  709.         break;
  710.           codemap[(int) p1->code] = 1;
  711.         }
  712.       if ((p1 == 0 || p1->code == UNKNOWN) && i >= 4)
  713.         {
  714.           in_switch = CODE_SWITCH;
  715.           printf (" switch (GET_CODE (x%d))\n  {\n", depth);
  716.         }
  717.     }
  718.  
  719.       if (in_switch == MODE_SWITCH)
  720.     {
  721.       if (modemap[(int) p->mode])
  722.         {
  723.           printf ("  case %smode:\n", GET_MODE_NAME (p->mode));
  724.           modemap[(int) p->mode] = 0;
  725.         }
  726.     }
  727.       if (in_switch == CODE_SWITCH)
  728.     {
  729.       if (codemap[(int) p->code])
  730.         {
  731.           printf ("  case ");
  732.           print_code (p->code);
  733.           printf (":\n");
  734.           codemap[(int) p->code] = 0;
  735.         }
  736.     }
  737.  
  738.       printf ("  if (");
  739.       if (p->exact || (p->code != UNKNOWN && in_switch != CODE_SWITCH))
  740.     {
  741.       if (p->exact)
  742.         printf ("x%d == %s", depth, p->exact);
  743.       else
  744.         {
  745.           printf ("GET_CODE (x%d) == ", depth);
  746.           print_code (p->code);
  747.         }
  748.       printf (" && ");
  749.     }
  750.       if (p->mode != VOIDmode && !ignmode && in_switch != MODE_SWITCH)
  751.     printf ("GET_MODE (x%d) == %smode && ",
  752.         depth, GET_MODE_NAME (p->mode));
  753.       if (p->test_elt_zero_int)
  754.     printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
  755.       if (p->veclen)
  756.     printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
  757.       if (p->test_elt_one_int)
  758.     printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
  759.       if (p->dupno >= 0)
  760.     printf ("rtx_equal_p (x%d, recog_operand[%d]) && ", depth, p->dupno);
  761.       if (p->tests)
  762.     printf ("%s (x%d, %smode)", p->tests, depth,
  763.         GET_MODE_NAME (p->mode));
  764.       else
  765.     printf ("1");
  766.  
  767.       if (p->opno >= 0)
  768.     printf (")\n    { recog_operand[%d] = x%d; ",
  769.         p->opno, depth);
  770.       else
  771.     printf (")\n    ");
  772.  
  773.       if (p->c_test)
  774.     printf ("if (%s) ", p->c_test);
  775.  
  776.       if (p->insn_code_number >= 0)
  777.     printf ("return %d;", p->insn_code_number);
  778.       else
  779.     printf ("goto L%d;", p->success->number);
  780.  
  781.       if (p->opno >= 0)
  782.     printf (" }\n");
  783.       else
  784.     printf ("\n");
  785.  
  786.       /* Now, if inside a switch, branch to next switch member
  787.      that might also need to be tested if this one fails.  */
  788.  
  789.       if (in_switch == CODE_SWITCH)
  790.     {
  791.       /* Find the next alternative to p
  792.          that might be applicable if p was applicable.  */
  793.       for (p1 = p->next; p1; p1 = p1->next)
  794.         if (p1->code == UNKNOWN || p->code == p1->code)
  795.           break;
  796.       if (p1 == 0 || p1->code == UNKNOWN)
  797.         printf ("  break;\n");
  798.       else if (p1 != p->next)
  799.         {
  800.           printf (" goto L%d;\n", p1->number);
  801.           p1->label_needed = 1;
  802.         }
  803.     }
  804.  
  805.       if (in_switch == MODE_SWITCH)
  806.     {
  807.       /* Find the next alternative to p
  808.          that might be applicable if p was applicable.  */
  809.       for (p1 = p->next; p1; p1 = p1->next)
  810.         if (p1->mode == VOIDmode || p->mode == p1->mode)
  811.           break;
  812.       if (p1 == 0 || p1->mode == VOIDmode)
  813.         printf ("  break;\n");
  814.       else if (p1 != p->next)
  815.         {
  816.           printf (" goto L%d;\n", p1->number);
  817.           p1->label_needed = 1;
  818.         }
  819.     }
  820.     }
  821.  
  822.   if (in_switch != NO_SWITCH)
  823.     printf ("  }\n");
  824.  
  825.   if (afterward)
  826.     {
  827.       change_state (pos, afterpos);
  828.       printf ("  goto L%d;\n", afterward);
  829.     }
  830.   else
  831.     printf ("  goto ret0;\n");
  832.  
  833.   for (p = tree; p; p = p->next)
  834.     if (p->success)
  835.       {
  836.       {
  837.         pos = p->position;
  838.         write_tree (p->success, pos,
  839.             p->afterward ? p->afterward->number : afterward,
  840.             p->afterward ? pos : afterpos,
  841.             0);
  842.       }
  843.       }
  844. }
  845.  
  846. void
  847. print_code (code)
  848.      RTX_CODE code;
  849. {
  850.   register char *p1;
  851.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  852.     {
  853.       if (*p1 >= 'a' && *p1 <= 'z')
  854.     putchar (*p1 + 'A' - 'a');
  855.       else
  856.     putchar (*p1);
  857.     }
  858. }
  859.  
  860. int
  861. same_codes (p, code)
  862.      register struct decision *p;
  863.      register RTX_CODE code;
  864. {
  865.   for (; p; p = p->next)
  866.     if (p->code != code)
  867.       return 0;
  868.  
  869.   return 1;
  870. }
  871.  
  872. void
  873. clear_codes (p)
  874.      register struct decision *p;
  875. {
  876.   for (; p; p = p->next)
  877.     p->code = UNKNOWN;
  878. }
  879.  
  880. int
  881. same_modes (p, mode)
  882.      register struct decision *p;
  883.      register enum machine_mode mode;
  884. {
  885.   for (; p; p = p->next)
  886.     if (p->mode != mode || p->tests)
  887.       return 0;
  888.  
  889.   return 1;
  890. }
  891.  
  892. void
  893. clear_modes (p)
  894.      register struct decision *p;
  895. {
  896.   for (; p; p = p->next)
  897.     p->ignmode = 1;
  898. }
  899.  
  900. void
  901. change_state (oldpos, newpos)
  902.      char *oldpos;
  903.      char *newpos;
  904. {
  905.   int odepth = strlen (oldpos);
  906.   int depth = odepth;
  907.   int ndepth = strlen (newpos);
  908.  
  909.   /* Pop up as many levels as necessary.  */
  910.  
  911.   while (strncmp (oldpos, newpos, depth))
  912.     --depth;
  913.  
  914.   /* Go down to desired level.  */
  915.  
  916.   while (depth < ndepth)
  917.     {
  918.       if (newpos[depth] == '*')
  919.     printf ("  x%d = recog_addr_dummy;\n  XEXP (x%d, 0) = x%d;\n",
  920.         depth + 1, depth + 1, depth);
  921.       else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
  922.     printf ("  x%d = XVECEXP (x%d, 0, %d);\n",
  923.         depth + 1, depth, newpos[depth] - 'a');
  924.       else
  925.     printf ("  x%d = XEXP (x%d, %c);\n",
  926.         depth + 1, depth, newpos[depth]);
  927.       ++depth;
  928.     }
  929. }
  930.  
  931. char *
  932. copystr (s1)
  933.      char *s1;
  934. {
  935.   register char *tem;
  936.  
  937.   if (s1 == 0)
  938.     return 0;
  939.  
  940.   tem = (char *) xmalloc (strlen (s1) + 1);
  941.   strcpy (tem, s1);
  942.  
  943.   return tem;
  944. }
  945.  
  946. void
  947. mybzero (b, length)
  948.      register char *b;
  949.      register int length;
  950. {
  951.   while (length-- > 0)
  952.     *b++ = 0;
  953. }
  954.  
  955. char *
  956. concat (s1, s2)
  957.      char *s1, *s2;
  958. {
  959.   register char *tem;
  960.  
  961.   if (s1 == 0)
  962.     return s2;
  963.   if (s2 == 0)
  964.     return s1;
  965.  
  966.   tem = (char *) xmalloc (strlen (s1) + strlen (s2) + 2);
  967.   strcpy (tem, s1);
  968.   strcat (tem, " ");
  969.   strcat (tem, s2);
  970.  
  971.   return tem;
  972. }
  973.  
  974. int
  975. xrealloc (ptr, size)
  976.      char *ptr;
  977.      int size;
  978. {
  979.   int result = realloc (ptr, size);
  980.   if (!result)
  981.     fatal ("virtual memory exhausted");
  982.   return result;
  983. }
  984.  
  985. int
  986. xmalloc (size)
  987. {
  988.   register int val = malloc (size);
  989.  
  990.   if (val == 0)
  991.     fatal ("virtual memory exhausted");
  992.   return val;
  993. }
  994.  
  995. void
  996. fatal (s, a1, a2)
  997.      char *s;
  998. {
  999.   fprintf (stderr, "genrecog: ");
  1000.   fprintf (stderr, s, a1, a2);
  1001.   fprintf (stderr, "\n");
  1002.   fprintf (stderr, "after %d instruction definitions\n",
  1003.        next_insn_code);
  1004.   exit (FATAL_EXIT_CODE);
  1005. }
  1006.  
  1007. /* More 'friendly' abort that prints the line and file.
  1008.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1009.  
  1010. void
  1011. fancy_abort ()
  1012. {
  1013.   fatal ("Internal gcc abort.");
  1014. }
  1015.  
  1016. int
  1017. main (argc, argv)
  1018.      int argc;
  1019.      char **argv;
  1020. {
  1021.   rtx desc;
  1022.   struct decision *tree = 0;
  1023.   FILE *infile;
  1024.   extern rtx read_rtx ();
  1025.   register int c;
  1026.  
  1027.   obstack_init (rtl_obstack);
  1028.  
  1029.   if (argc <= 1)
  1030.     fatal ("No input file name.");
  1031.  
  1032.   infile = fopen (argv[1], "r");
  1033.   if (infile == 0)
  1034.     {
  1035.       perror (argv[1]);
  1036.       exit (FATAL_EXIT_CODE);
  1037.     }
  1038.  
  1039.   init_rtl ();
  1040.   next_insn_code = 0;
  1041.  
  1042.   printf ("/* Generated automatically by the program `genrecog'\n\
  1043. from the machine description file `md'.  */\n\n");
  1044.  
  1045.   /* Read the machine description.  */
  1046.  
  1047.   while (1)
  1048.     {
  1049.       c = read_skip_spaces (infile);
  1050.       if (c == EOF)
  1051.     break;
  1052.       ungetc (c, infile);
  1053.  
  1054.       desc = read_rtx (infile);
  1055.       if (GET_CODE (desc) == DEFINE_INSN)
  1056.     tree = merge_trees (tree, make_insn_sequence (desc));
  1057.       if (GET_CODE (desc) == DEFINE_PEEPHOLE
  1058.       || GET_CODE (desc) == DEFINE_EXPAND)
  1059.     next_insn_code++;
  1060.     }
  1061.  
  1062.   printf ("#include \"config.h\"\n");
  1063.   printf ("#include \"rtl.h\"\n");
  1064. #if defined( DSP56000 ) || defined( DSP96000 )
  1065.   printf ("#if defined( _INTELC32_ )\n" );
  1066.   printf ("#include \"iconfig.h\"\n");
  1067.   printf ("#else\n" );
  1068.   printf ("#include \"insn-config.h\"\n");
  1069.   printf ("#endif\n" );
  1070. #else
  1071.   printf ("#include \"insn-config.h\"\n");
  1072. #endif
  1073.   printf ("#include \"recog.h\"\n");
  1074.   printf ("#include \"real.h\"\n");
  1075.   printf ("\n\
  1076. /* `recog' contains a decision tree\n\
  1077.    that recognizes whether the rtx X0 is a valid instruction.\n\
  1078. \n\
  1079.    recog returns -1 if the rtx is not valid.\n\
  1080.    If the rtx is valid, recog returns a nonnegative number\n\
  1081.    which is the insn code number for the pattern that matched.\n");
  1082.   printf ("   This is the same as the order in the machine description of\n\
  1083.    the entry that matched.  This number can be used as an index into\n\
  1084.    insn_templates and insn_n_operands (found in insn-output.c)\n\
  1085.    or as an argument to output_insn_hairy (also in insn-output.c).  */\n\n");
  1086.  
  1087.   printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
  1088.   printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
  1089.   printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
  1090.   printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
  1091.   printf ("extern rtx recog_addr_dummy;\n\n");
  1092.   printf ("#define operands recog_operand\n\n");
  1093.  
  1094.   break_out_subroutines (tree);
  1095.  
  1096.   printf ("int\nrecog (x0, insn)\n     register rtx x0;\n     rtx insn;\n{\n");
  1097.   printf ("  register rtx x1, x2, x3, x4, x5;\n  rtx x6, x7, x8, x9, x10, x11;\n");
  1098.   printf ("  int tem;\n");
  1099.  
  1100.   write_tree (tree, "", 0, "", 1);
  1101.   printf (" ret0: return -1;\n}\n");
  1102.  
  1103.   fflush (stdout);
  1104.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  1105. }
  1106.